package com.ruoyi.waterManagement.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.business.waterManagement.WrStatvolM;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.waterManagement.domain.vo.NzyWrStatvolMVO;
import com.ruoyi.waterManagement.domain.vo.RzsWrStatvolHwrVO;
import com.ruoyi.waterManagement.domain.vo.WaterReportVO;
import com.ruoyi.waterManagement.service.IWaterRegimeMonitoringService;
import com.ruoyi.waterManagement.service.IWrStatvolHService;
import com.ruoyi.waterManagement.service.IWrStatvolMService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 测站月水量数据Controller
 * 
 * @author hwd
 * @date 2023-02-20
 */
@RestController
@RequestMapping("/WrStatvolM")
public class WrStatvolMController extends BaseController
{
    @Autowired
    private IWrStatvolMService wrStatvolMService;

    @Autowired
    private IWrStatvolHService wrStatvolHService;

    @Autowired
    private IWaterRegimeMonitoringService waterRegimeMonitoringService;
    /**
     * 查询测站月水量数据列表
     */
    @RequiresPermissions("waterManagement:WrStatvolM:list")
    @GetMapping("/list")
    public TableDataInfo list(WrStatvolM wrStatvolM)
    {
        startPage();
        List<WrStatvolM> list = wrStatvolMService.selectWrStatvolMList(wrStatvolM);
        if (list.size()>0){
            String statNm= waterRegimeMonitoringService.queryStatNm(wrStatvolM.getStatCd());
            list.forEach(WrStatvolM->WrStatvolM.setStatNm(statNm));
        }
        return getDataTable(list);
    }

    /**
     * 导出测站月水量数据列表
     */
    @RequiresPermissions("waterManagement:WrStatvolM:export")
    @Log(title = "测站月水量数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WrStatvolM wrStatvolM)
    {
        List<WrStatvolM> list = wrStatvolMService.selectWrStatvolMList(wrStatvolM);
        ExcelUtil<WrStatvolM> util = new ExcelUtil<WrStatvolM>(WrStatvolM.class);
        util.exportExcel(response, list, "测站月水量数据数据");
    }


    /**
     * 导出水管机构月报
     */
    @RequiresPermissions("waterManagement:WrStatvolD:export")
    @Log(title = "水管机构月报", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMonthWaterReport")
    public AjaxResult exportMonthWaterReport(HttpServletResponse response, WaterReportVO waterReportVO) throws IOException {
        if (waterReportVO.getOrgNm()==null){
            return AjaxResult.error("请选择水管机构");
        }
        wrStatvolMService.exportMonthWaterReport(waterReportVO, response);
        return AjaxResult.success("1");
    }

    /**
     * 获取测站月水量数据详细信息
     */
    @RequiresPermissions("waterManagement:WrStatvolM:query")
    @GetMapping(value = "/{statCd}")
    public AjaxResult getInfo(@PathVariable("statCd") String statCd)
    {
        return success(wrStatvolMService.selectWrStatvolMByStatCd(statCd));
    }

    /**
     * 新增测站月水量数据
     */
    @RequiresPermissions("waterManagement:WrStatvolM:add")
    @Log(title = "测站月水量数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WrStatvolM wrStatvolM)
    {
        return toAjax(wrStatvolMService.insertWrStatvolM(wrStatvolM));
    }

    /**
     * 修改测站月水量数据
     */
    @RequiresPermissions("waterManagement:WrStatvolM:edit")
    @Log(title = "测站月水量数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WrStatvolM wrStatvolM)
    {
        return toAjax(wrStatvolMService.updateWrStatvolM(wrStatvolM));
    }

    /**
     * 删除测站月水量数据
     */
    @RequiresPermissions("waterManagement:WrStatvolM:remove")
    @Log(title = "测站月水量数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{statCds}")
    public AjaxResult remove(@PathVariable String[] statCds)
    {
        return toAjax(wrStatvolMService.deleteWrStatvolMByStatCds(statCds));
    }

    /**
     * 查询取用水测站年逐月水量信息列表
     */

    @GetMapping("/selectNzyWrStatvolMVO")
    public AjaxResult list(NzyWrStatvolMVO nzyWrStatvolMVO)
    {

        /**入参水管机构、日期
         */
        //判断关键参数日期是否存在
        Date wrdate= nzyWrStatvolMVO.getMon();
        String yy ;
        Calendar date = Calendar.getInstance();
        if(wrdate==null||wrdate.after(DateUtils.getNowDate())){
            //晚于当前日期或者没有日期，则改为当前日期
            nzyWrStatvolMVO.setMon(DateUtils.getNowDate());
             date.setTime(new Date());
             yy = String.valueOf(date.get(Calendar.YEAR));
        }else {
           date.setTime(wrdate);
            yy = String.valueOf(date.get(Calendar.YEAR));
        }
        nzyWrStatvolMVO.setYy(yy);
        if (StringUtils.isEmpty(nzyWrStatvolMVO.getOrgCd())) {
            nzyWrStatvolMVO.setOrgCd(null);
        }
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(nzyWrStatvolMVO.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        nzyWrStatvolMVO.setOrgCd(resCode+"%");
        List<NzyWrStatvolMVO> nzylist = wrStatvolMService.selectNzyWrStatvolMVO(nzyWrStatvolMVO);
        if (grade == 1){
            //查找下属水管机构
            resCode =  resCode + "%0000";
        }else if (grade == 2) {
            //查找下属水管机构
            resCode =resCode + "%00";
        }else if(grade ==3) {
            resCode =resCode+"%";
        }else {
            resCode = resCode;
        }
        //查找下属机构
        RzsWrStatvolHwrVO rzsWrStatvolHwrVO=new RzsWrStatvolHwrVO();
        rzsWrStatvolHwrVO.setOrgCd(resCode);
        List<RzsWrStatvolHwrVO> orglist = wrStatvolHService.listOrgCd(rzsWrStatvolHwrVO);
        List<NzyWrStatvolMVO> orgnewList =new ArrayList<>();
        //遍历查询下属水管机构统计
        for (RzsWrStatvolHwrVO r:orglist
        ) {
            HashMap<String, Object> stringObjectHashMap = BusinessUtils.getAddvAndOrgCodeInfo(r.getOrgCd());
            String ro = (String) stringObjectHashMap.get("resCode");
            List<NzyWrStatvolMVO> nzyWrStatvolMVOList = nzylist.stream().filter(s -> s.getOrgCd().startsWith(ro)).collect(Collectors.toList());
            NzyWrStatvolMVO wrVoSum =new NzyWrStatvolMVO();
            wrVoSum.setOrgNm(r.getOrgNm());
            BigDecimal s1 =new BigDecimal("0");
            BigDecimal s2 =new BigDecimal("0");
            BigDecimal s3 =new BigDecimal("0");
            BigDecimal s4 =new BigDecimal("0");
            BigDecimal s5 =new BigDecimal("0");
            BigDecimal s6 =new BigDecimal("0");
            BigDecimal s7 =new BigDecimal("0");
            BigDecimal s8 =new BigDecimal("0");
            BigDecimal s9 =new BigDecimal("0");
            BigDecimal s10 =new BigDecimal("0");
            BigDecimal s11 =new BigDecimal("0");
            BigDecimal s12 =new BigDecimal("0");
            BigDecimal sc =new BigDecimal("0");
            if (nzyWrStatvolMVOList.size()>0) {
                for (NzyWrStatvolMVO w : nzyWrStatvolMVOList
                ) {
                    s1 = w.getS1() == null ? s1.add(new BigDecimal("0")) : s1.add(new BigDecimal(w.getS1())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s2 = w.getS2() == null ? s2.add(new BigDecimal("0")) : s2.add(new BigDecimal(w.getS2())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s3 = w.getS3() == null ? s3.add(new BigDecimal("0")) : s3.add(new BigDecimal(w.getS3())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s4 = w.getS4() == null ? s4.add(new BigDecimal("0")) : s4.add(new BigDecimal(w.getS4())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s5 = w.getS5() == null ? s5.add(new BigDecimal("0")) : s5.add(new BigDecimal(w.getS5())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s6 = w.getS6() == null ? s6.add(new BigDecimal("0")) : s6.add(new BigDecimal(w.getS6())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s7 = w.getS7() == null ? s7.add(new BigDecimal("0")) : s7.add(new BigDecimal(w.getS7())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s8 = w.getS8() == null ? s8.add(new BigDecimal("0")) : s8.add(new BigDecimal(w.getS8())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s9 = w.getS9() == null ? s9.add(new BigDecimal("0")) : s9.add(new BigDecimal(w.getS9())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s10 = w.getS10() == null ? s10.add(new BigDecimal("0")) : s10.add(new BigDecimal(w.getS10())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s11 = w.getS11() == null ? s11.add(new BigDecimal("0")) : s11.add(new BigDecimal(w.getS11())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s12 = w.getS12() == null ? s12.add(new BigDecimal("0")) : s12.add(new BigDecimal(w.getS12())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    sc = w.getSc() == null ? sc.add(new BigDecimal("0")) : sc.add(new BigDecimal(w.getSc()));
                }
            }
                wrVoSum.setOrgCd("合计");
                wrVoSum.setS1(s1.toString());
                wrVoSum.setS2(s2.toString());
                wrVoSum.setS3(s3.toString());
                wrVoSum.setS4(s4.toString());
                wrVoSum.setS5(s5.toString());
                wrVoSum.setS6(s6.toString());
                wrVoSum.setS7(s7.toString());
                wrVoSum.setS8(s8.toString());
                wrVoSum.setS9(s9.toString());
                wrVoSum.setS10(s10.toString());
                wrVoSum.setS11(s11.toString());
                wrVoSum.setS12(s12.toString());
                wrVoSum.setSc(sc.toString());
            orgnewList.add(0, wrVoSum);
            }
        orgnewList.removeAll(Collections.singleton(null));
            //将数据结果放入map中区分时
            Collections.reverse(orgnewList);
        //将数据结果放入map中区分时
        return AjaxResult.success(orgnewList);
    }

    /**
     * 查询水管机构水量月报
     */
    @GetMapping("/listMonthWaterReport")
    public AjaxResult listMonthWaterReport(WaterReportVO monthWaterReportVO){
        //将数据结果放入map中区分时
        return AjaxResult.success(wrStatvolMService.selectMonthWaterReport(monthWaterReportVO));
    }
}
